<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ShopProduct
 *
 * @author jasonzv
 */
require_once ("DB.php");

class ShopProduct {

    private $title;
    private $producerMainName;
    private $producerFirstName;
    protected $price;
    private $discount;

    function __construct($title, $firstName, $mainName, $price) {
        $this->title = $title;
        $this->producerFirstName = $firstName;
        $this->producerMainName = $mainName;
        $this->price = $price;
    }

    public function getProducerFirstName() {
        return $this->producerFirstName;
    }

    public function getProducerMainName() {
        return $this->producerMainName;
    }

    public function setDiscount($num) {
        $this->discount = $num;
    }

    public function getDiscount() {
        return $this->discount;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getPrice() {
        return $this->price;
    }

    public function getProducer() {
        return "{$this->producerFirstName}" . "{$this->producerMainName}";
    }

    //is public because no xxxxx defined
    function getSummaryLine() {
        $base = "{$this->title} ({$this->producerMainName}, ";
        $base.="{$this->producerFirstName})";
        return $base;
    }

    public static function getInstance($id, DB_common $db) {

        $query = "select * from products where id='$id'";
        $query_result = $db->query($query);

        if (DB::isError($query_result)) {
            die($query_result->getMessage());
        }
        $row = $query_result->fetchRow(DB_FETCHMODE_ASSOC);
        if (empty($row)) {
            return null;
        }

        if ($row['type'] == "book") {
            $product = new BookProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['numpages']);
        } else if ($row['type'] == 'cd') {
            $product=new CdProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playlength']);
        }else{
            $product= new ShopProduct($row['title'], $row['firstname'], $row['mainname'], $row['price']);
            
        }
        $product->setId($row['id']);
        $product->setDiscount($row['id']);
        return $product;
    }

}

/* is_bool()
 * is_integer()
 * is_resource()
 * is_string()
 * is_null()
 * 
 */
?>
